<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='类型' name='type' :rules="[{required : true, message : '请选择类型'}]">
            <a-radio-group v-model:value="formData.type" name="type">
                <a-radio v-for="item in appTypes" :key="item.value" :value="item.value">{{ item.label }}</a-radio>
            </a-radio-group>
        </a-form-item>
        <a-form-item label='名称' name='name' :rules="[{required : true, message : '请填写名称'}]">
            <a-input type='text' v-model:value='formData.name' />
        </a-form-item>
        <a-form-item label='图标' name='cover'>
            <a-upload list-type='picture-card' :max-count="1" v-model:file-list='coverList' :beforeUpload='beforeUpload' :customRequest='uploadCover'>
                <template v-if='coverList.length == 0'>
                    <PlusOutlined />
                </template>
            </a-upload>
        </a-form-item>
        <a-form-item label='跳转方式' name='linkType'>
            <a-radio-group v-model:value="formData.linkType" name="linkType">
                <a-radio v-for="item in linkTypes" :key="item.value" :value="item.value">{{ item.label }}</a-radio>
            </a-radio-group>
        </a-form-item>
        <a-form-item v-if="formData.linkType == 1" label='链接' name='url'>
            <a-input type='text' v-model:value='formData.url' />
        </a-form-item>
        <a-form-item v-if="formData.linkType == 2" label='二维码' name='qr'>
            <a-input type='hidden' v-model:value='formData.qr' />
            <a-upload list-type='picture-card' :max-count="1" v-model:file-list='qrList' :beforeUpload='beforeUpload' :customRequest='uploadQr'>
                <template v-if='qrList.length == 0'>
                    <PlusOutlined />
                </template>
            </a-upload>
        </a-form-item>
        <a-form-item label='排序' name='sort'>
            <a-input type='number' v-model:value='formData.sort' />
        </a-form-item>
        <a-form-item label='启用' name='enable'>
            <a-switch v-model:checked='formData.enable' checked-children='启用' un-checked-children='禁用'/>
        </a-form-item>
    </a-form>
</template>

<script lang="ts" setup>
    import { appTypeApi, linkTypepeApi, saveApi } from '@/api/Apps';
import { uploadApi } from '@/api/Upload';
import { PlusOutlined } from '@ant-design/icons-vue';
import { nextTick, ref } from 'vue';


    const props = defineProps<{
        row? : any
    }>()

    const coverList = ref<any[]>([])
    const qrList = ref<any[]>([])

    const formRef = ref()

    
    const formData = ref({
        id : 0,
        type : '',
        name : '',
        cover : '',
        linkType : '',
        url : '',
        qr : '',
        sort : 0,
        enable : true

    })

    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                formData.value[k] = props.row[k]
            }
        }

        if(props.row.cover) {
            coverList.value[0] = {
                status : 'done',
                url : import.meta.env.B_NET_STATIC_URL + props.row.cover
            }
        }

        if(props.row.linkType == 2 && props.row.qr) {
            qrList.value[0] = {
                status : 'done',
                url : import.meta.env.B_NET_STATIC_URL + props.row.qr
            }
        }
    }


    async function submit() {
        let params  = await formRef.value.validate()
        if('sort' in params && params.sort === '') {
            params.sort = 0
        }


        return saveApi(params)
    }

    //app类型
    const appTypes = ref<any[]>([])
    //跳转类型
    const linkTypes = ref<any[]>([])

    function uploadCover(e) {
        uploadApi(e.file).then(res => {
            coverList.value[0].status = 'done'
            coverList.value[0].url = import.meta.env.B_NET_STATIC_URL + res.data
            formData.value.cover = res.data
        })
    }

    function uploadQr(e) {
        uploadApi(e.file).then(res => {
            qrList.value[0].status = 'done'
            qrList.value[0].url = import.meta.env.B_NET_STATIC_URL + res.data
            qrList.value[0].value = res.data

            formData.value.qr = res.data
        })
    }

    function beforeUpload(file:File, field:FormField) {
        return true
    }

    appTypeApi().then(r => {
        if(formData.value.type == '') {
            formData.value.type = Number(r.data[0].code)
        }
        appTypes.value = r.data.map(item => ({label : item.name, value : Number(item.code)}))
    })

    linkTypepeApi().then(r => {
        if(formData.value.linkType == '') {
            formData.value.linkType = Number(r.data[0].code)
        }
        
        linkTypes.value = r.data.map(item => ({label : item.name, value : Number(item.code)}))
    })

    defineExpose({
        submit
    })
</script>